/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;

import com.sinodata.bsm.common.vo.ProtocolParameter;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-23 上午11:17:57          tangli         1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class WmiUtil {
    private static final Logger logger = Logger.getLogger(WmiUtil.class);
    private static final Set<String> loaclIpSet;
    private static String VBS_PATH = null;
    static {
        loaclIpSet = getLocalIp();
        File file = new File("wmi");
        VBS_PATH = file.getAbsolutePath();
    }

    public WmiUtil() {
    }

    /**
     * 
     * @param ip
     * @param scriptName
     * @param args 除了ip、账号、密码之外的参数
     * @return
     */
    public static BufferedReader execWmiScript(String ip, String scriptName, String[] args) {
        ProtocolParameter protocolParameter = ProtocolParameterUtil.getWmiValue(ip);
        if (protocolParameter == null) {
            return null;
        }
        String username = protocolParameter.getUserName();
        String password = protocolParameter.getPwd();
        BufferedReader reader = null;
        String otherArg = "";
        if (args != null && args.length > 0) {
            for (String s : args) {
                otherArg += "\"" + s + "\"" + " ";
            }
        }
        //执行命令是： cscript CM_CPU.vbs 172.168.2.23 wangpeng wp_1234 额外参数,如果访问本地主机ip改为127.0.0.1
        //E:\\vbs\\表示vbs脚本在服务器的E:\\vbs\\目录下
        String cmd = "cscript \"" + VBS_PATH + "\\" + scriptName + "\" " + ip + " " + username + " " + password + " " + otherArg;
        //如果是本地IP则执行命令是:script CM_CPU.vbs 172.0.0.1 额外参数
        if (loaclIpSet.contains(ip)) {
            //cmd = "cscript \"" + VBS_PATH + "\\" + scriptName + "\" 127.0.0.1" + " " + otherArg;
            ip = "127.0.0.1";
            cmd = "cscript \"" + VBS_PATH + "\\" + scriptName + "\" " + ip + " " + otherArg;
        }
        logger.info(cmd);
        try {
            reader = CommandReader.getLocalReader(cmd, 0, 0);
        } catch (IOException e) {
            logger.error("failed to execute wmi script", e);
        }
        return reader;
    }

    public static BufferedReader execWmiScript(String ip, String username, String password, String scriptName, String[] args) {
        BufferedReader reader = null;
        String otherArg = "";
        if (args != null && args.length > 0) {
            for (String s : args) {
                otherArg += "\"" + s + "\"" + " ";
            }
        }
        if (loaclIpSet.contains(ip)) {
            //cmd = "cscript \"" + VBS_PATH + "\\" + scriptName + "\" 127.0.0.1" + " " + otherArg;
            ip = "127.0.0.1";
        }
        //执行命令是： cscript CM_CPU.vbs 172.168.2.23 wangpeng wp_1234 额外参数,如果访问本地主机ip改为127.0.0.1
        //E:\\vbs\\表示vbs脚本在服务器的E:\\vbs\\目录下
        String cmd = "cscript \"" + VBS_PATH + "\\" + scriptName + "\" " + ip + " " + username + " " + password + " " + otherArg;
        //如果是本地IP则执行命令是:script CM_CPU.vbs 172.0.0.1 额外参数
        logger.info(cmd);
        try {
            reader = CommandReader.getLocalReader(cmd, 0, 0);
        } catch (IOException e) {
            logger.error("failed to execute wmi script", e);
        }
        return reader;
    }

    /**
     * 该方法用于获取本地所有IP地址
     */
    private static Set<String> getLocalIp() {
        Set<String> ipSet = new HashSet<String>();
        Enumeration<NetworkInterface> netInterfaces = null;
        try {
            netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = netInterfaces.nextElement();
                Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ipSet.add(ips.nextElement().getHostAddress());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ipSet;
    }
}
